import time

from selenium import webdriver

# 创建 ChromeOptions 对象，用于配置 Chrome 浏览器的启动参数
options = webdriver.ChromeOptions()

# 添加参数指定 Chrome 浏览器使用的用户数据目录
# 这里指向了 Windows 系统下的默认用户数据目录
options.add_argument("--user-data-dir=C:/Users/20486/AppData/Local/Google/Chrome/User Data/Default")
# 创建浏览器对象
driver = webdriver.Chrome(options=options)

# 打开特定网页
driver.get('https://www.goofish.com')
# 用于将浏览器窗口最大化
driver.maximize_window()
# 等待页码加载
time.sleep(5)
# 获取查找搜索框并且输入内容
input_element = driver.find_element("xpath", "//input[@class='search-input--WY2l9QD3']")
input_element.send_keys("vivo x200")

# 查找搜索按钮元素并点击
search_button = driver.find_element("xpath", "//button[@class='search-icon--bewLHteU']")
# 点击
search_button.click()

for page in range(1, 51):
    time.sleep(5)
    goods_list = driver.find_elements("xpath", "//a[@class='feeds-item-wrap--rGdH_KoF']")
    for goods in goods_list:
        try:
            title = goods.find_element("xpath", ".//span[@class='main-title--sMrtWSJa']").text
            price = goods.find_element("xpath", ".//span[@class='number--NKh1vXWM']").text
            print(title, price, type(price))
        except Exception as e:
            print(e)

    # 模拟滑动动作
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(1)
    # 模拟人点击翻页
    driver.find_element("xpath", "//button[@class='search-pagination-arrow-container--lt2kCP6J'][2]").click()

driver.quit()